import * as types from './mutations_types'
import { getJobListAPI, getListPlateBlockListAPI } from '@/api/system/job'

export default {
  GetJobList({ commit, state }, params = { request: true }) { // 获取岗位列表-请求数据：默认必须走接口请求
    const { jobList } = state
    const { request } = params
    return new Promise((resolve, reject) => {
      if (request || JSON.stringify(jobList) === '[]') {
        getJobListAPI({
          ...params
        }).then(response => {
          const result = response
          if (+result.code === 200) {
            commit(types.SET_JOBS_LIST, result.data || [])
            resolve(result)
          } else {
            reject(new Error(result.message || '获取列表失败！'))
          }
        }).catch(error => {
          reject(error.message || '获取列表失败！')
        })
      } else {
        resolve({
          data: jobList,
          message: '交易成功',
          status: '0000'
        })
      }
    })
  },
  GetListPlateBlockList({ commit, state }, params = { request: true }) { // 获取岗位列表-请求数据：默认必须走接口请求
    const { listPlateBlock } = state
    const { request } = params
    return new Promise((resolve, reject) => {
      if (request || JSON.stringify(listPlateBlock) === '[]') {
        getListPlateBlockListAPI({
          ...params
        }).then(response => {
          const result = response
          if (+result.code === 200) {
            commit(types.SET_PLATE_LIST, result.data || [])
            resolve(result)
          } else {
            reject(new Error(result.message || '获取列表失败！'))
          }
        }).catch(error => {
          reject(error.message || '获取列表失败！')
        })
      } else {
        resolve({
          data: listPlateBlock,
          message: '交易成功',
          status: '0000'
        })
      }
    })
  }
}
